<?php

namespace App\Models\UserModels;

use App\Models\ParentModel;
use DB;
use Log;

/**
 * 抖音私信model
 * @author puppet
 */
class PrivateLetter extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_dy_privateletter';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_dy_privateletter';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;

    //拉取数据20条	type	0视频，否则主页
    function getFansComment($openid,$popenid,$page=1){
        $user_id=isset(session('vip_user')[0]['admin_id']) ?  intval(session('vip_user')[0]['admin_id']) : 0;
        $size = 20;
        $offset = $size * ($page - 1);
        //先查询一次看下私信表有没有数据再进行下一步
        $res=DB::select("select id from pt_dy_privateletter where toopenid='$openid'");
    //    $rea=isset($res[0]['id']) ? $res : false;
        if($res){
            $count=DB::select("select count(id) as count from pt_dy_privateletter where popenid='$popenid' and toopenid='$openid'");
            $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
            $re=DB::select("select a.*,b.avatar,b.nickname,c.nickname as fname,c.avatar as tx  from pt_dy_privateletter as a left join pt_user as b on a.popenid=b.open_id left join pt_fans as c on a.toopenid=c.open_id  and a.popenid='$popenid' and a.toopenid='$openid' order by a.id desc limit {$offset},{$size}");
          // print_r($re);exit();
            $re=isset($re[0]['id']) ? $re : false;

            $html="";
            if($re){
                foreach($re as $k=>$v){
                    $html.="<tr>";
                    $html.="<td><a href=\"javascript:void(0);\" target=\"_blank\"><img src=\"{$v['avatar']}\" width=\"50px\" height=\"50px\"></a></td>";
                    $html.="<td>{$v['nickname']}</td>";
                    $html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['content']}</td>";
                    $html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['create_time']}</td>";
                    $html.="<td><a href=\"javascript:void(0);\" target=\"_blank\"><img src=\"{$v['tx']}\" width=\"50px\" height=\"50px\"></a></td>";
                    $html.="<td>{$v['fname']}</td>";
                    $html.="</tr>";
                }
            }
            return ["html"=>$html];
        }

    }

    //拉取数据20条	type	0视频，否则主页
    function getGzComment($openid,$popenid,$page=1){
        $user_id=isset(session('vip_user')[0]['admin_id']) ?  intval(session('vip_user')[0]['admin_id']) : 0;
        $size = 20;
        $offset = $size * ($page - 1);
        //先查询一次看下私信表有没有数据再进行下一步
        $res=DB::select("select id from pt_dy_privateletter where toopenid='$openid'");
        if($res){
            $count=DB::select("select count(id) as count from pt_dy_privateletter where popenid='$popenid' and toopenid='$openid'");
            $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
            $re=DB::select("select a.*,b.avatar,b.nickname,c.nickname as fname,c.avatar as tx  from pt_dy_privateletter as a left join pt_user as b on a.popenid=b.open_id left join pt_following as c on a.toopenid=c.open_id  and a.popenid='$popenid' and a.toopenid='$openid' order by a.id desc limit {$offset},{$size}");
            //   print_r($re);exit();
            $re=isset($re[0]['id']) ? $re : false;
            $html="";
            if($re){
                foreach($re as $k=>$v){
                    $html.="<tr>";
                    $html.="<td><a href=\"javascript:void(0);\" target=\"_blank\"><img src=\"{$v['avatar']}\" width=\"50px\" height=\"50px\"></a></td>";
                    $html.="<td>{$v['nickname']}</td>";
                    $html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['content']}</td>";
                    $html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['create_time']}</td>";
                    $html.="<td><a href=\"javascript:void(0);\" target=\"_blank\"><img src=\"{$v['tx']}\" width=\"50px\" height=\"50px\"></a></td>";
                    $html.="<td>{$v['fname']}</td>";
                    $html.="</tr>";
                }
            }
            return ["html"=>$html];
        }

    }

}

